package cn.ctodb.divination.service;

import cn.ctodb.divination.domain.Bysl;
import cn.ctodb.divination.repository.ByslRepository;
import cn.ctodb.divination.repository.search.ByslSearchRepository;
import cn.ctodb.divination.service.dto.ByslDTO;
import cn.ctodb.divination.service.mapper.ByslMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Bysl.
 */
@Service
@Transactional
public class ByslService {

    private final Logger log = LoggerFactory.getLogger(ByslService.class);

    private final ByslRepository byslRepository;

    private final ByslMapper byslMapper;

    private final ByslSearchRepository byslSearchRepository;

    public ByslService(ByslRepository byslRepository, ByslMapper byslMapper, ByslSearchRepository byslSearchRepository) {
        this.byslRepository = byslRepository;
        this.byslMapper = byslMapper;
        this.byslSearchRepository = byslSearchRepository;
    }

    /**
     * Save a bysl.
     *
     * @param byslDTO the entity to save
     * @return the persisted entity
     */
    public ByslDTO save(ByslDTO byslDTO) {
        log.debug("Request to save Bysl : {}", byslDTO);
        Bysl bysl = byslMapper.byslDTOToBysl(byslDTO);
        bysl = byslRepository.save(bysl);
        ByslDTO result = byslMapper.byslToByslDTO(bysl);
        byslSearchRepository.save(bysl);
        return result;
    }

    /**
     * Get all the bysls.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<ByslDTO> findAll() {
        log.debug("Request to get all Bysls");
        List<ByslDTO> result = byslRepository.findAll().stream()
            .map(byslMapper::byslToByslDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     * Get one bysl by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public ByslDTO findOne(Long id) {
        log.debug("Request to get Bysl : {}", id);
        Bysl bysl = byslRepository.findOne(id);
        ByslDTO byslDTO = byslMapper.byslToByslDTO(bysl);
        return byslDTO;
    }

    /**
     * Delete the  bysl by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Bysl : {}", id);
        byslRepository.delete(id);
        byslSearchRepository.delete(id);
    }

    /**
     * Search for the bysl corresponding to the query.
     *
     * @param query the query of the search
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<ByslDTO> search(String query) {
        log.debug("Request to search Bysls for query {}", query);
        return StreamSupport
            .stream(byslSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(byslMapper::byslToByslDTO)
            .collect(Collectors.toList());
    }

    public ByslDTO findByNum(int num) {
        Bysl bysl = byslRepository.findByNum(num);
        ByslDTO byslDTO = byslMapper.byslToByslDTO(bysl);
        return byslDTO;
    }
}
